﻿using MediatR;
using SqlSugar;
using WarehouseManagement.Application.Command.AllocateCommand;
using WarehouseManagement.Domain.DTO.Allocate;
using WarehouseManagement.Domain.DTO.RequestTransfer;
using WarehouseManagement.Domain.DTO.ShipApply;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.AllocateCommandHandler
{
    public class AllocateSelectCommandHandler : IRequestHandler<AllocateSelectCommand, PageDTO<SelectAllocateDto>>
    {
        private readonly IAllocateRepository _allocateRep;
        private readonly IAllocationProductsRepository _allocationProductsRep;
        protected readonly ISqlSugarClient db;

        public AllocateSelectCommandHandler(IAllocateRepository allocateRep, IAllocationProductsRepository allocationProductsRep, ISqlSugarClient db)
        {
            this._allocateRep = allocateRep;
            this._allocationProductsRep = allocationProductsRep;
            this.db = db;
        }

        public async Task<PageDTO<SelectAllocateDto>> Handle(AllocateSelectCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<Allocate, AllocationProducts>((a, b) => new JoinQueryInfos(
            JoinType.Inner, a.ANumber == b.ANumber))
            .Where((a, b) => a.IsDel == false)
            .GroupBy((a, b) => new { a.AllocateId, a.AName, a.ANumber, a.ADate, a.AWarehousePersonnel, a.AGoWarehouse, a.WarehouseId, a.ARecallProduct, a.AState, a.ProductId })
            .Select((a, b) => new SelectAllocateDto
            {
                AllocateId = a.AllocateId,
                AName = a.AName,
                ANumber = a.ANumber,
                ADate = a.ADate,
                AWarehousePersonnel = a.AWarehousePersonnel,
                AGoWarehouse = a.AGoWarehouse,
                WarehouseId = a.WarehouseId,
                ARecallProduct = a.ARecallProduct,
                ProductId = a.ProductId,
                AState = a.AState,
                AllocationProductsId= SqlFunc.AggregateMax(b.AllocationProductsId),
                APAllocateNum = SqlFunc.AggregateMax(b.APAllocateNum),
                APInboundWarehouse = SqlFunc.AggregateMax(b.APInboundWarehouse),
                PNumber = SqlFunc.AggregateMax(b.PNumber),
                PName = SqlFunc.AggregateMax(b.PName),
                PSpec = SqlFunc.AggregateMax(b.PSpec),
                PUnit = SqlFunc.AggregateMax(b.PUnit),
                WName = SqlFunc.AggregateMax(b.WName),
                SPConfirmNum = SqlFunc.AggregateMax(b.SPConfirmNum),
            });
            if (!string.IsNullOrEmpty(request.AName))
            {
                list = list.Where(a => a.AName.Contains(request.AName));
            }
            if (!string.IsNullOrEmpty(request.ANumber))
            {
                list = list.Where(a => a.ANumber.Contains(request.ANumber));
            }
            var totalCount = await list.CountAsync();

            // 应用分页  
            var preproductionlist = await list
                .OrderByDescending(a => a.AllocateId)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToListAsync();

            // 准备结果  
            var pages = new PageDTO<SelectAllocateDto>
            {
                TotalCount = totalCount,
                PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize),
                list = preproductionlist
            };
            return pages;
        }
    }
}
